import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_learn_vi/src/bean/bean_penting.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/4.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
class MineItem4Page extends StatefulWidget {
  //构造函数
  MineItem4Page({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MineItem2State();
  }
}

class _MineItem2State extends State<MineItem4Page>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  //标签页面保活 或者称保持状态
  @override
  bool get wantKeepAlive => true;

  List<PentingBean> _list = [];

  AnimationController _animationController;

  Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();

    _animationController = new AnimationController(
        vsync: this, duration: Duration(milliseconds: 1200));

    _animation = new Tween(begin: Offset(0, -1), end: Offset(0, 0)).animate(
        CurvedAnimation(
            curve: Curves.fastOutSlowIn, parent: _animationController));

    getNewsList();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (_list == null || _list.length == 0) {
      return Container(
        child: NoDataWidget(
          clickCallBack: () {
            getNewsList();
          },
        ),
      );
    }
    //一个列表 切记不要再使用 Scaffold
    //否则会拦截事件 失去滑动联动效果
    return Container(
      padding: EdgeInsets.only(top: 12),
      color: Colors.white,
      child: RefreshIndicator(
        onRefresh: () {
          return getNewsList(isRefresh: true);
        },
        child: ListView.builder(
          //去除内边距
          padding: EdgeInsets.all(0),
          //列表子Item个数
          itemCount: _list.length,
          //每个子Item的布局构建
          itemBuilder: (BuildContext context, int index) {
            return buildBodyWidget(index);
          },
        ),
      ),
    );
  }

  //每个子Item的布局构建
  Widget buildBodyWidget(int index) {
    PentingBean newsBean = _list[index];

    //水波纹点击事件监听
    return InkWell(
      //手指点击抬起时的回调
      onTap: () {
        //showWebViewPage(context: context, pageUrl: newsBean.detailUrl);
      },
      child: Container(
        padding: EdgeInsets.only(left: 10, right: 10),
        constraints: BoxConstraints(maxHeight: 130),
        color: Colors.white,
        //线性布局左右排列
        child: Row(
          //主轴方向开始对齐 在这里是左对齐
          mainAxisAlignment: MainAxisAlignment.start,
          //交叉轴上开始对齐 在这里是顶部对齐
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //左侧的图片
            buildLeftImage(index, newsBean),
            //右侧的文本区域
            buildRightTextArea(index, newsBean)
          ],
        ),
      ),
    );
  }

  ///左侧的图片区域
  Container buildLeftImage(int index, PentingBean newsBean) {
    //状态默认为 0 审核中； 1 审核通过，2审核拒绝
    int statue = newsBean.pStatue;
    String text = "";
    Color color;
    if (statue == 0) {
      //审核中
      color = Colors.blue;
      text = "审核中";
    } else if (statue == 1) {
      //审核通过
      color = Colors.green;
      text = "审核通过";
    } else {
      //审核拒绝
      color = Colors.redAccent;
      text = "审核拒绝";
    }

    return Container(
      margin: EdgeInsets.only(right: 12),
      width: 80,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: 1,
            height: 6,
            color: index == 0 ? Colors.transparent : color,
          ),
          ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(2)),
            child: Container(
              padding: EdgeInsets.only(left: 8, right: 8, bottom: 2),
              color: color,
              child: Text(
                "$text",
                style: TextStyle(fontSize: 10, color: Colors.white),
              ),
            ),
          ),
          Container(
            width: 1,
            height: 66,
            constraints: BoxConstraints(maxHeight: 88),
            color: index == _list.length - 1 ? Colors.transparent : color,
          ),
        ],
      ),
    );
  }

  ///右侧的文本区域
  Expanded buildRightTextArea(int index, PentingBean newsBean) {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            height: 4,
          ),
          Text(
            "${newsBean.createTime} ",
            softWrap: true,
            overflow: TextOverflow.ellipsis,
            maxLines: 1,
            textAlign: TextAlign.right,
            style: TextStyle(fontSize: 14, color: Colors.black38),
          ),
          SizedBox(
            height: 20,
          ),
          Text(
            buildTitleFunction(newsBean),
            softWrap: true,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 16),
          )
        ],
      ),
    );
  }

  Future<bool> getNewsList({bool isRefresh = false}) async {
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.LISTPENDINGMY,
    );
    if (responseInfo.success) {
      _list = [];
      Map map = responseInfo.data;
      List list = map["list"];
      list.forEach((element) {
        PentingBean newsBean = PentingBean.fromMap(element);
        _list.add(newsBean);
      });
    }

    setState(() {});
    if (isRefresh) {
      ToastUtils.showToast("已刷新");
    }

    return true;
  }

  String buildTitleFunction(PentingBean newsBean) {
    return "${newsBean.pContent}";
  }
}
